//
//  ZCSaveViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/23.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCSaveViewController.h"
#import <Masonry.h>
#import "ZCDetailListModel.h"
#import "ZCBaseSqliteTool.h"
#import "ZCPlayMusicTool.h"
#import "ZCSetUpModel.h"

@interface ZCSaveViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation ZCSaveViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        self.dataArr = [NSMutableArray array];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.dataArr = [[ZCBaseSqliteTool shareZCMusicDataTool] selectAllMusicModel];
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"3187"]];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.editButtonItem setImage:[UIImage imageNamed:@"iconfont-shanchu"]];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [ self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *rec = @"re";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rec];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:rec];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    ZCDetailListModel *model = self.dataArr[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.singerName;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"我的收藏";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
     [super setEditing:editing animated:animated];
    if (editing) {
        [self.editButtonItem setTitle:@""];
        [self.editButtonItem setImage:[UIImage imageNamed:@"iconfont-xiugai"]];
        
    }else {
        [self.editButtonItem setTitle:@""];
        [self.editButtonItem setImage:[UIImage imageNamed:@"iconfont-shanchu"]];
    }
    [self.tableView setEditing:editing animated:YES];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        ZCDetailListModel *model = self.dataArr[indexPath.row];
        [[ZCBaseSqliteTool shareZCMusicDataTool] deleteMusic:model];
        self.dataArr = [[ZCBaseSqliteTool shareZCMusicDataTool] selectAllMusicModel];
        [self.tableView reloadData];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"取消收藏";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCSetUpModel *model = self.dataArr[indexPath.row];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:model.name,@"name",model.singerName,@"nameSinge",model.auditionList,@"url",nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"play" object:self userInfo:dic];
    
    [ZCPlayMusicTool getPlay:model.auditionList];
    [ZCPlayMusicTool sharePlay].urlMusic = model.auditionList;

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
